import pandas
from sklearn import tree
import pydotplus
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
import matplotlib.image as pltimg
df = pandas.read_csv("决策树数据集.csv")
features = ['vowel_sum','duplication','commonality','1 try','2 tries','3 tries','4 tries','5 tries','6 tries','7 or more tries (X)']
X = df[features]
y = df['label_3']
dtree = DecisionTreeClassifier() # 默认采用基尼系数创建决策树对象   
#dtree = DecisionTreeClassifier(criterion='entropy')采用信息熵创建决策树对象
dtree = dtree.fit(X, y) # 训练
data = tree.export_graphviz(dtree, out_file=None, feature_names=features) # 决策树生成数据
graph = pydotplus.graph_from_dot_data(data) # 用数据生成图片
#gini表示基尼系数，samples表示该节点所含样本数量，value表示不同类别的个数有多少。
graph.write_png('mydecisiontree.png')
img=pltimg.imread('mydecisiontree.png')
imgplot = plt.imshow(img)
plt.show()


# 预测准确率
y_predict = dc_tree.predict(x)
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y,y_predict)
print(accuracy)